//
//  WBHomePictureView.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let baseTag = 1024
let statusImageClikedNotification = "statusImageClikedNotification"

class WBHomePictureView: WBBaseView {

    var statusViewModel: WBHomeViewModel? {
        didSet {
            //先把所有的imageView隐藏
            for i in 0..<9 {
                let imageView = self.viewWithTag(baseTag+i)
                imageView?.isHidden = true
            }
            
            //遍历所有图片,取imageView, 将图片装到该imageView里
            guard let pic_urls = statusViewModel?.picUrls, pic_urls.count > 0 else {
                return
            }
            
            var tag = baseTag
            //遍历图片数组
            for pictureModel in pic_urls {
                //如果有四张图片,就跳过第三个imageView 换种布局方式
                if pic_urls.count == 4 && tag-baseTag == 2 {
                    tag += 1
                }
                
                //给imageView赋值
                let imageView = self.viewWithTag(tag) as! UIImageView
                imageView.wb_setImageWith(urlStr: pictureModel.thumbnail_pic!, placeHolder: "avatar_default_big")
                
                //如果imageView有图片,则将该imageView显示出来
                imageView.isHidden = false
                //开启交互
                imageView.isUserInteractionEnabled = true
                //添加手势
                let tap =  UITapGestureRecognizer(target: self, action: #selector(imageViewTaped(tap:)))
                imageView.addGestureRecognizer(tap)
                
                //如果只有一张图片, 需要将imageview设为原图大小, 否则,调整为原始大小
                if pic_urls.count == 1 {
                    imageView.frame.size = (statusViewModel?.pictureViewSize)!
                }else {
                    imageView.frame.size = statusLayout.imageSize
                }
                
                //偏移
                tag += 1
            }
        }
    }
    
    override func wbSetupSubviews() {
        super.wbSetupSubviews()
        setupUI ()
    }

}

// MARK: - 设置UI
extension WBHomePictureView {
    func setupUI () {
        //设置背景色
        backgroundColor = UIColor.clear
        
        //图片横向,或纵向, 每增加一行/列的偏移量
        let offset = statusLayout.imageSize.width + statusLayout.margin
        
        //纵向 y = 行数 * (imageview.height + 间隔)
        //横向 x = 列数 * (imageView.width + 间隔)
        //第一张图片的frame
        let firstImageFrame = CGRect(origin: CGPoint.zero, size: statusLayout.imageSize)
        
        //添加9个图片视图
        for i in 0..<9 {
            //列
            let col = i % statusLayout.numPerRow
            //行
            let row = i / statusLayout.numPerRow
            
            //计算每个imageView的frame
            let rect = firstImageFrame.offsetBy(dx: offset*CGFloat(col), dy: offset*CGFloat(row))
            let imageView = UIImageView()
            imageView.frame = rect
            imageView.backgroundColor = UIColor.white
            imageView.tag = baseTag+i
            
            //保证图片不会被拉伸或压瘪
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            
            //添加到父容器中
            addSubview(imageView)
        }
        
        //clipsToBounds: 将超出父视图的子视图裁切掉
        self.clipsToBounds = true
        
    }
}

// MARK: - 设置手势
extension WBHomePictureView {
    func imageViewTaped (tap: UITapGestureRecognizer) {
        //1. 拿到被点击图片的index
        guard let tag = tap.view?.tag, let picmodels = statusViewModel?.picUrls else {
            return
        }
        
        var index = tag - baseTag
        //四宫格时
        if (statusViewModel?.picUrls)!.count == 4 && index > 2 {
            index -= 1
        }
        
        //??特定返回数组 [String]
        let urls = (picmodels as NSArray).value(forKey: "middle_pic")
        
        //创建通知
        let notification = Notification(name: Notification.Name(rawValue: statusImageClikedNotification), object: nil, userInfo:["index": index, "urls": urls])
        //发送通知
        NotificationCenter.default.post(notification)
    }
}
